package demoMod.nanaki.patches.events.city;

import basemod.ReflectionHacks;
import com.evacipated.cardcrawl.modthespire.lib.SpirePatch;
import com.evacipated.cardcrawl.modthespire.lib.SpireReturn;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.events.city.KnowingSkull;
import com.megacrit.cardcrawl.localization.EventStrings;
import demoMod.nanaki.NanakiMod;
import demoMod.nanaki.characters.NanakiCharacter;

public class KnowingSkullPatch {
    public static final String ID = NanakiMod.makeID("Knowing Skull");
    private static final EventStrings eventStrings;
    public static final String NAME;
    public static final String[] DESCRIPTIONS;
    public static final String[] OPTIONS;

    @SpirePatch(
            clz = KnowingSkull.class,
            method = "buttonEffect"
    )
    public static class PatchButtonEffect {
        public static SpireReturn<Void> Prefix(KnowingSkull event, int buttonPressed) {
            if (AbstractDungeon.player instanceof NanakiCharacter) {
                try {
                    Enum screen = ReflectionHacks.getPrivate(event, KnowingSkull.class, "screen");
                    if (screen.name().equals("INTRO_1")) {
                        NanakiMod.addToBot(new AbstractGameAction() {
                            @Override
                            public void update() {
                                boolean disabled = AbstractDungeon.player.gold < 35;
                                event.imageEventText.setDialogOption(String.format(OPTIONS[disabled ? 9 : 2], (int) Math.ceil(AbstractDungeon.player.maxHealth * 0.25)), disabled);
                                isDone = true;
                            }
                        });
                    } else if (screen.name().equals("ASK")) {
                        if (buttonPressed == 4) {
                            AbstractDungeon.player.loseGold(35);
                            AbstractDungeon.player.heal((int) Math.ceil(AbstractDungeon.player.maxHealth * 0.25));
                            event.imageEventText.updateBodyText(DESCRIPTIONS[8]);
                            event.imageEventText.clearAllDialogs();
                            event.imageEventText.setDialogOption(OPTIONS[8]);
                            ReflectionHacks.setPrivate(event, KnowingSkull.class, "screen", Enum.valueOf((Class<Enum>) Class.forName("com.megacrit.cardcrawl.events.city.KnowingSkull$CurScreen"), "COMPLETE"));
                            return SpireReturn.Return(null);
                        }
                    }
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
            return SpireReturn.Continue();
        }
    }

    static {
        eventStrings = CardCrawlGame.languagePack.getEventString(ID);
        NAME = eventStrings.NAME;
        DESCRIPTIONS = eventStrings.DESCRIPTIONS;
        OPTIONS = eventStrings.OPTIONS;
    }
}
